<?php
Yii::import('application.modules.menu.models.*');

class menuService {
    
    public static function listRecipies($model){
        if ($model !== null){
            $list = "<select name='Recipiedetail[materialid]' id='Recipiedetail_materialid' class='styledselect_form_1'>";
            foreach ($model as $k => $v){
                $list .= "<option value='".$v->recipieid."'>".$v->drink->drinkname."</option>";
            }
            return $list .= "</select>";
        }        
    }
    
    public static function getMaterialName($materialID){
        $materialModel = Material::model()->find(array(
            'select' => 'materialname',
            'condition' => 'materialid = :id',
            'params' => array(
                ':id' => $materialID,
            )
        ));
        return $materialModel->materialname;
    }
    
    public static function getDrinkName($drinkID){
        $drinkModel = Recipie::model()->find(array(
            'select' => 'drinkname',
            'condition' => 'drinkid = :id',
            'params' => array(
                ':id' => $drinkID,
            )
        ));
    }
    
    public static function getDrinkNameByRecipieID($recipieID){
        $recipieModel = Recipie::model()->find(array(            
            'condition' => 'recipieid = :id',
            'params' => array(
                ':id' => $recipieID,
            )
        ));
        return $recipieModel->drink->drinkname;
    }
    
    public static function getCurrentMenyVersion(){
        $config = Config::model()->find(array(
            'condition' => 'name = "Menu Version"'
        ));
        
        return $config->value;
    }
    
    public static function getAmountCoffeesByOrderID($orderID){
        $order = Orderdetail::model()->find(array(
            'select' => 'count(orderid) as sumOrder',
            'condition' => 'orderid = :orderID',
            'params' => array(
                ':orderID' => $orderID,
            )
        ));
        return $order->sumOrder;
    }
    
    public static function getSyrupNameByID($syrupid, $printPrice=false){
        $syrupModel = Syrup::model()->find(array(
            'condition' => 'syrupid = :id',
            'params' => array(
                ':id' => $syrupid,
            )
        ));
        if (!$printPrice)
            return $syrupModel->syrupname;
        else    return $syrupModel->syrupprice;
    }
    
    public static function getMaterailListOfRecipie($recipieid){
        $materialList = Recipiedetail::model()->findAll(array(
            'condition' => 'recipieid = :recipieid',
            'params' => array(
                ':recipieid' => $recipieid,
            )
        ));
        
        foreach ($materialList as $material => $value){
            $list .= self::getMaterialName($value->materialid).", ";
        }
        $list = substr_replace($list, '', -1);
        return $list;
    }
    
    public static function getUserList(){
        $users = User::model()->findAll();
        return CHtml::listData($users, 'userid', 'username');
    }
    
    public static function getMaterialUnit($unitID){
        $unit = Unit::model()->find(array(
            'select' => 'unitname',
            'condition' => 'unitid = :id',
            'params' => array(
                ':id' => $unitID,
            )
        ));
        return $unit->unitname;
    }
    
}

?>
